package com.bestcem.xm.member.controller.v2;

import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.component.security.dto.TokenDTO;
import com.bestcem.xm.member.controller.v2.param.DownloadMemberQuery;
import com.bestcem.xm.member.service.ExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 联系人数据下载
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/8/9
 */
@Slf4j
@RestController
@RequestMapping("member/v2/download")
public class DownloadController {

    /**
     * 导出service
     */
    @Resource
    private ExportService exportService;


    /**
     * 个人联系人导出
     *
     * @param query
     * @return
     */
    @PostMapping("/ind")
    public XmResultJson indDownloadMember(@Valid @RequestBody DownloadMemberQuery query,
                                          HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        
        TokenDTO token = SecurityContextHolder.getToken();
        query.setOrgId(orgId);
        query.setUserId(userId);
        query.setSuperRole(token.getSuperRole());
        query.setIsSenior(token.getIsSenior());
        return exportService.exportMember(query);
    }

    /**
     * 机构 联系人导出
     *
     * @param query
     * @return
     */
    @PostMapping("/ins")
    public XmResultJson insDownloadMember(@Valid @RequestBody DownloadMemberQuery query,
                                              HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        
        TokenDTO token = SecurityContextHolder.getToken();
        query.setOrgId(orgId);
        query.setUserId(userId);
        query.setSuperRole(token.getSuperRole());
        query.setIsSenior(token.getIsSenior());
        return exportService.exportMember(query);
    }

    /**
     * 内部联系人导出
     *
     * @param query
     * @return
     */
    @PostMapping("/int")
    public XmResultJson intDownloadMember(@Valid @RequestBody DownloadMemberQuery query,
                                              HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        
        TokenDTO token = SecurityContextHolder.getToken();
        query.setOrgId(orgId);
        query.setUserId(userId);
        query.setSuperRole(token.getSuperRole());
        query.setIsSenior(token.getIsSenior());
        return exportService.exportMember(query);
    }

}
